package com.usian.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.article.mapper.ApArticleMapper;
import com.usian.article.service.ApArticleService;
import com.usian.model.article.ApArticle;
import com.usian.model.article.dtos.ArticleHomeDto;
import com.usian.model.common.dtos.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    @Autowired
    private ApArticleMapper apArticleMapper;

    private final static short MAX_PAGE_SIZE = 50;

    @Value("${fileServerUrl}")
    String fileServerUrl;


    @Override
    public ResponseResult load(Short loadtype, ArticleHomeDto dto) {

        //1：校验参数
        Integer size = dto.getSize();
        if(size == null || size == 0){
            size = 10;
        }
        size = Math.min(size,MAX_PAGE_SIZE);
        dto.setSize(size);

        //2:校验 上拉 、下拉
        if(!loadtype.equals(1)&&!loadtype.equals(2)){
            loadtype = 1;
        }

        //频道
        if(StringUtils.isEmpty(dto.getTag())){
            dto.setTag("__all__");
        }

        if(dto.getMaxBehotTime() == null) dto.setMaxBehotTime(new Date());
        if(dto.getMinBehotTime() == null) dto.setMinBehotTime(new Date());

        List<ApArticle> apArticleList = apArticleMapper.loadArticleList(dto, loadtype);

        ResponseResult responseResult = ResponseResult.okResult(apArticleList);
        responseResult.setHost(fileServerUrl);
        return responseResult;
    }
}

